#!/usr/bin/env python
# encoding: utf-8

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from win32api import GetSystemMetrics

class drawWidget(QWidget):
    def __init__(self, parent=None):
        super(drawWidget, self).__init__(parent)
        self.setAutoFillBackground(True)
        #  palette = self.palette()
        #  palette.setColor(QPalette.Background, QColor(255, 255, 255))
        #  self.setPalette(palette)
        #  self.setWindowFlags(Qt.FramelessWindowHint)# 无窗体
        #  self.setAttribute(Qt.WA_TranslucentBackground)# 背景透明
        self.TextFont      = QFont("Arial", 10, QFont.Bold)
        self.stepTextFont      = QFont("Arial", 10, QFont.Bold)
        self.pen = [
            QPen(QColor(145,44,238)),
            QPen(QColor(132,112,255)),
            QPen(QColor(32,205,187)),
            QPen(QColor(255,0,255)),
            QPen(QColor(255,127,0)),
            QPen(QColor(255,215,0)),
        ]
        self.color = [
            QColor(145,44,238),
            QColor(132,112,255),
            QColor(32,205,187),
            QColor(255,0,255),
            QColor(255,127,0),
            QColor(255,215,0),
        ]
        self.step =[3,4,3]
        self.pos = [0,40,80,100]
        self.raySize = 10
        self.SSD = 100
        self.direction = 'x'

        self.pdd_oar_ray = 'pdd'
        if self.pdd_oar_ray == 'pdd':
            self.setFixedSize(600, 300)
        if self.pdd_oar_ray == 'oar':
            self.setFixedSize(600, 300)
        if self.pdd_oar_ray == 'ray':
            self.setFixedSize(600, 600)

    def paintEvent(self,e):
        painter = QPainter()
        painter.begin(self)
        painter.setRenderHints(QPainter.Antialiasing|QPainter.HighQualityAntialiasing)#设置反锯齿
        if self.pdd_oar_ray == 'oar':
            self.drawOarStep(painter)
        if self.pdd_oar_ray == 'pdd':
            self.drawPddStep(painter)
        if self.pdd_oar_ray == 'ray':
            self.drawRay(painter)
        if self.pdd_oar_ray == 'direction':
            self.drawDirection(painter)
        painter.end()

    def drawOarStep(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (7 / 10) * h
        step = self.step
        pos = self.pos
        if not len(pos)>1:
            return
        max_x = max(pos)
        # 移动坐标到左侧
        painter.translate(w,h)
        # 坐标转换比例系数
        perc = d_x_1/max(pos)
        painter.setFont(QFont("Arial", 8, QFont.Bold))
        #  painter.drawText(0, -h/2, d_x_1/1.8,-d_y_1/10, Qt.AlignCenter, '分步')
        # 放射源水平线
        # 画坐标轴
        #  painter.setFont(self.TextFont);
        painter.setPen(QPen(QColor(100, 0, 0), 2));
        painter.drawLine(-max_x*perc, d_y_1 , max_x*perc,d_y_1 ) #上
        for i in range(-max_x-1,max_x+1):
            if self.raySize<30:
                if i%10== 0:
                    # 画10位置的刻度线
                    painter.setPen(QPen(QColor(250, 0, 0), 2));
                    painter.drawLine(i*perc, d_y_1 , i*perc,d_y_1 -10)
                    painter.drawText(i*perc, d_y_1 , d_x_1/3, d_y_1/2, Qt.AlignLeft, '%s'%i)
            else:
                if i%20== 0:
                    painter.setPen(QPen(QColor(250, 0, 0), 2));
                    painter.drawLine(i*perc, d_y_1 , i*perc,d_y_1 -10)
                    painter.drawText(i*perc-10, d_y_1 , d_x_1/3, d_y_1/2, Qt.AlignLeft, '%s'%i)

        # 所有坐标位置
        pos_x = []
        for i in range(1,len(pos)):
            w = pos[i]-pos[i-1]
            end = 0
            h = d_y_1*(5-i)/5
            if i == 1:
                painter.drawLine(0, d_y_1, 0, -h)
                painter.drawText(pos[i-1]*perc,  -h*1.5, d_y_1/3, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i-1])
            else:
                # 正
                painter.drawText(pos[i-1]*perc+d_y_1/4,  -h*1.5, d_y_1/3, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i-1])
                # 负
                painter.drawText(-pos[i-1]*perc-d_y_1/2, -h*1.5, d_y_1/3, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i-1])
            painter.setPen(self.pen[i-1])
            # 正 顶部线
            painter.drawLine(pos[i-1]*perc, -h, pos[i]*perc, -h)
            # 负 顶部线
            painter.drawLine(-pos[i-1]*perc, -h, -pos[i]*perc, -h)
            for j in range(1,w+1):
                if j < w:
                    if j%step[i-1]== 0:
                        p = pos[i-1]+j
                        pos_x.append(p)
                        # 正
                        painter.drawLine(p*perc, d_y_1, p*perc, -h)
                        # 负
                        painter.drawLine(-p*perc, d_y_1, -p*perc, -h)
            p = pos[i-1]+j
            pos_x.append(p)
            painter.drawLine(p*perc, d_y_1, p*perc, -h)
            painter.drawLine(-p*perc, d_y_1, -p*perc, -h)

    def drawPddStep(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (7 / 10) * h

        step = self.step
        pos = self.pos
        if not len(pos)>1:
            return

        max_x = max(pos)
        # 移动坐标到左侧
        painter.translate(w-d_x_1,h)
        # 坐标转换比例系数
        perc = d_x_1/max(pos)*2
        painter.setFont(QFont("Arial", 8, QFont.Bold))
        # 放射源水平线
        # 画坐标轴
        #  painter.setFont(self.TextFont);
        painter.setPen(QPen(QColor(100, 0, 0), 2));
        painter.drawLine(0, d_y_1 , max_x*perc,d_y_1 ) #上
        for i in range(0,max_x+1):
            if i%10== 0:
                # 画10位置的刻度线
                painter.setPen(QPen(QColor(250, 0, 0), 2));
                painter.drawLine(i*perc, d_y_1 , i*perc,d_y_1 -10)
                painter.drawText(i*perc, d_y_1 , d_x_1/3, d_y_1/2, Qt.AlignLeft, '%s'%i)

        # 所有坐标位置
        pos_x = []
        painter.setFont(self.stepTextFont);
        for i in range(1,len(pos)):
            w = pos[i]-pos[i-1]
            end = 0
            h = d_y_1*(5-i)/5
            if i == 1:
                painter.drawLine(0, d_y_1, 0, -h)
                painter.drawText(pos[i-1]*perc+d_y_1/2, -h*1.5, d_y_1/2, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i-1])
            else:
                painter.drawText(pos[i-1]*perc+d_y_1/2, -h*1.5, d_y_1/2, d_y_1/2, Qt.AlignLeft, '%.1f'%step[i-1])
            painter.setPen(self.pen[i-1])
            painter.drawLine(pos[i-1]*perc, -h, pos[i]*perc, -h)
            for j in range(1,w+1):
                if j < w:
                    if j%step[i-1]== 0:
                        p = pos[i-1]+j
                        pos_x.append(p)
                        # 正
                        painter.drawLine(p*perc, d_y_1, p*perc, -h)
                        # 负
                        #  painter.drawLine(-p*perc, axis_y, -p*perc, -h)
            p = pos[i-1]+j
            pos_x.append(p)
            painter.drawLine(p*perc, d_y_1, p*perc, -h)

    def drawRay(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 移动坐标系到中心
        painter.translate(w,h)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (9 / 10) * h
        painter.setFont(QFont("Arial", 12*d_x_1/1.8/w, QFont.Bold))
        # 画矩形 - 水箱
        painter.setBrush(QColor(125, 255, 255));
        painter.drawRect(-d_x_1, 0, 2*d_x_1, d_y_1);
        # 中心线
        painter.drawLine(0, -d_y_1, 0, d_y_1)
        d_x_2 = (5 / 10) * w * (self.raySize/60)
        d_y_2 = (5 / 10) * h
        # 放射源水平线
        # painter.drawLine(-d_x_2, 0, d_x_2,0)
        painter.drawLine(-d_x_2, -d_y_1 , d_x_2,-d_y_1 ) #上
        painter.drawText(-d_x_1/4, -d_y_1 , d_x_1/1.8,-d_y_1/10, Qt.AlignCenter, '射线照射')
        # 射野-红色
        painter.setPen(QPen(QColor(0, 0, 100), 5));
        painter.drawLine(-d_x_2, 0, d_x_2,0)
        painter.drawText(-d_x_2, d_y_2/3 , d_x_1/1.8,-d_y_1/10, Qt.AlignCenter, '射野:%.1fcm'%self.raySize)
        # 射野光线边缘线
        painter.setPen(QPen(QColor(200, 0, 0), 3));
        per = d_x_2/d_y_1
        d_x_3 = (d_y_1+d_y_1/2)*per
        d_y_3 = d_y_1/2
        painter.drawLine(0, -d_y_1, d_x_3 , d_y_3) #左斜线
        painter.drawLine(0, -d_y_1, -d_x_3 , d_y_3) #右斜线
        # SSD
        painter.setPen(QPen(QColor(0, 100, 0), 3));
        painter.drawLine(d_x_2, 0, d_x_2, -d_y_1)
        painter.drawText(d_x_2, -d_y_1/2, d_x_1/2,-d_y_1/10, Qt.AlignCenter, '源波距:%.1fcm'%self.SSD)

    def drawDirection(self,painter):
        h = self.height()/2
        w = self.width()/2
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        # 移动坐标系到中心
        painter.translate(w,h)
        # 系数
        d_x_1 = (9 / 10) * w
        d_y_1 = (9 / 10) * h
        painter.setFont(QFont("Arial", 12*d_x_1/1.8/w, QFont.Bold))
        # 画矩形 - 水箱
        painter.setBrush(QColor(125, 255, 255));
        painter.drawRect(-d_x_1, -d_y_1, 2*d_x_1, 2*d_y_1);
        # 画矩形 - 射野
        painter.setBrush(QColor(25, 5, 155));
        painter.drawRect(-d_x_1*2/3, -d_y_1*2/3, 2*d_x_1*2/3, 2*d_y_1*2/3);
        painter.drawText(-d_x_1/4, -d_y_1 , d_x_1/1.8,-d_y_1/10, Qt.AlignCenter, '方向顶视图')
        # 画xy - 射野
        #  painter.setBrush(QColor(25, 255, 255));
        painter.setPen(QPen(QColor(0, 200, 0), 3));
        painter.drawLine(-d_x_1, 0, d_x_1, 0);
        painter.drawLine(0, -d_x_1, 0, d_x_1);
        painter.drawLine(w*9/10, 0, w*8/10, h/9);
        painter.drawLine(w*9/10, 0, w*8/10, -h/9);
        painter.drawLine(0, -h*9/10, w/9, -h*8/10);
        painter.drawLine(0, -h*9/10, -w/9, -h*8/10);
        painter.setFont(QFont("Arial", 10, QFont.Bold))
        painter.drawText(d_x_1/1.5, 0,w/5,h/5, Qt.AlignCenter, 'x')
        painter.drawText(0,-d_y_1/1.1,w/5,h/5, Qt.AlignCenter, 'y')
        # 中心线
        painter.setPen(QPen(QColor(200, 0, 0), 3));
        if self.direction == '西南->东北':
            painter.drawLine(-d_x_1*2/3, d_y_1*2/3, d_x_1*2/3, -d_y_1*2/3);
            painter.drawLine(0, 0, -d_x_1/8, 0);
            painter.drawLine(0, 0, 0, d_x_1/8);
        if self.direction == '东北->西南':
            painter.drawLine(-d_x_1*2/3, d_y_1*2/3, d_x_1*2/3, -d_y_1*2/3);
            painter.drawLine(0, 0, d_x_1/8, 0);
            painter.drawLine(0, 0, 0, -d_x_1/8);
        if self.direction == '东南->西北':
            painter.drawLine(-d_x_1*2/3, -d_y_1*2/3, d_x_1*2/3, d_y_1*2/3);
            painter.drawLine(0, 0, d_x_1/8, 0);
            painter.drawLine(0, 0, 0, d_x_1/8);
        if self.direction == '西北->东南':
            painter.drawLine(-d_x_1*2/3, -d_y_1*2/3, d_x_1*2/3, d_y_1*2/3);
            painter.drawLine(0, 0, -d_x_1/8, 0);
            painter.drawLine(0, 0, 0, -d_x_1/8);
        if self.direction == '西->东':
            painter.drawLine(-d_x_1*2/3, 0, d_x_1*2/3, 0);
            painter.drawLine(0, 0, -d_x_1/8, d_x_1/8);
            painter.drawLine(0, 0, -d_x_1/8, -d_x_1/8);
        if self.direction == '东->西':
            painter.drawLine(d_x_1*2/3, 0, -d_x_1*2/3, 0);
            painter.drawLine(0, 0, d_x_1/8, d_x_1/8);
            painter.drawLine(0, 0, d_x_1/8, -d_x_1/8);
        if self.direction == '南->北':
            painter.drawLine(0, d_y_1*2/3, 0 ,-d_y_1*2/3);
            painter.drawLine(0, 0, -d_y_1/8, d_y_1/8);
            painter.drawLine(0, 0, d_y_1/8, d_y_1/8);
        if self.direction == '北->南':
            painter.drawLine(0, d_y_1*2/3, 0 ,-d_y_1*2/3);
            painter.drawLine(0, 0, -d_y_1/8, -d_y_1/8);
            painter.drawLine(0, 0, d_y_1/8, -d_y_1/8);

    def updatePddRoad(self,step=[2,5,10],pos=[0,10,30,300]):
        self.step = step
        self.pos = pos
        self.update()

    def updateOarRoad(self,step=[10,3,5],pos=[0,40,60,80]):
        self.step = step
        self.pos = pos
        self.update()

    def updateRaySize(self,size):
        self.raySize = size
        self.update()

    def updateSSD(self,ssd):
        self.SSD = ssd
        self.update()

    def updataAngle(self):
        self.currentValue=self.currentValue+9
        self.m_angle=self.m_angle+6
        # self.m_angle=m_angle+36
        if self.m_angle >360:
            self.m_angle = 0
        # self.currentValue=10
        if self.currentValue >self.maxValue:
            self.currentValue = 0
        self.update()

    def reject(self):
        QDialog.reject(self)

class PddDialog(QDialog):
    def __init__(self,parent=None):
        super(QDialog, self).__init__(parent)
        #  import PyQt5_stylesheets
        #  self.setStyleSheet(PyQt5_stylesheets.load_stylesheet_pyqt5(style="style_blue"))
        self.resize(QSize(600,400))
        self.setWindowTitle('PDD任务设置')

        self.btn_ok = QPushButton('确认')
        self.btn_ok.clicked.connect(self.accept)
        self.btn_cancel = QPushButton('取消')
        self.btn_cancel.clicked.connect(self.reject)

        self.radio_pdd = QRadioButton('pdd')
        self.radio_pdd.setChecked(True)
        self.radio_pdd.toggled.connect(self.PddOar)
        self.radio_oar = QRadioButton('oar')
        self.radio_oar.toggled.connect(self.PddOar)

        Layout_PddOar = QHBoxLayout()
        Layout_PddOar.addWidget(self.radio_pdd)
        Layout_PddOar.addWidget(self.radio_oar)
        groupBox_PddOar = QGroupBox()
        groupBox_PddOar.setTitle('测量模式')
        groupBox_PddOar.setLayout(Layout_PddOar)
        groupBox_PddOar.setMaximumHeight(100)

        self.lab_target = QLabel('目标(mm):')
        self.lineEdit_target = QLineEdit()
        self.lab_step= QLabel('步距(mm):')
        self.lineEdit_step = QLineEdit()
        self.listView = QListWidget()
        self.listView.clicked.connect(self.listViewClicked)
        # 限制输入框只能输入数字
        self.lineEdit_step.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_step))
        self.lineEdit_target.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_target))

        self.btn_add = QPushButton('添加')
        self.btn_add.clicked.connect(self.add)
        self.btn_dec = QPushButton('移除')
        self.btn_dec.clicked.connect(self.dec)
        self.btn_default = QPushButton('恢复默认')
        self.btn_default.clicked.connect(self.default)

        gridLayout_road = QGridLayout()
        gridLayout_road.addWidget(self.lab_target,      1, 0, 1, 1)
        gridLayout_road.addWidget(self.lineEdit_target, 1, 1, 1, 1)
        gridLayout_road.addWidget(self.lab_step,        1, 2, 1, 1)
        gridLayout_road.addWidget(self.lineEdit_step,   1, 3, 1, 1)
        gridLayout_road.addWidget(self.btn_add,         1, 4, 1, 1)
        gridLayout_road.addWidget(self.btn_dec,         1, 5, 1, 1)
        gridLayout_road.addWidget(self.btn_default,     1, 6, 1, 1)
        gridLayout_road.addWidget(self.listView,        2, 0, 2, 7)
        groupBox_road = QGroupBox()
        groupBox_road.setTitle('走位路线设置')
        groupBox_road.setLayout(gridLayout_road)

        self.lab_Electrometer= QLabel('光机:')
        self.comboBox_Electrometer= QComboBox()
        self.comboBox_Electrometer.addItem('电子')
        self.comboBox_Electrometer.addItem('Co')
        self.comboBox_Electrometer.addItem('光机')
        self.lab_Power = QLabel('能量(MV/MeV):')
        self.comboBox_Power = QComboBox()
        self.comboBox_Power.addItem('1.0')
        self.comboBox_Power.addItem('1.2')
        self.comboBox_Power.addItem('2.0')

        self.lab_raySize = QLabel('射野(2-60cm):')
        self.lineEdit_raySize = QLineEdit('10')
        self.lineEdit_raySize.textChanged.connect(self.raySizeChanged)
        self.lineEdit_raySize.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_raySize))
        self.lab_SSD = QLabel('源波距(cm):')
        self.lineEdit_SSD = QLineEdit('100')
        self.lineEdit_SSD.textChanged.connect(self.SSDChanged)
        self.lineEdit_SSD.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_SSD))
        self.lab_deepth = QLabel('oar测量深度(mm):')
        self.lineEdit_deepth = QLineEdit('100')
        self.lineEdit_deepth.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_deepth))
        #  self.lineEdit_deepth.textChanged.connect(self.deepthChanged)

        gridLayout_ray = QGridLayout()
        gridLayout_ray.addWidget(self.lab_Electrometer,      0, 0, 1, 1)
        gridLayout_ray.addWidget(self.comboBox_Electrometer, 0, 1, 1, 1)
        gridLayout_ray.addWidget(self.lab_Power,             0, 2, 1, 1)
        gridLayout_ray.addWidget(self.comboBox_Power,        0, 3, 1, 1)
        gridLayout_ray.addWidget(self.lab_raySize,           1, 0, 1, 1)
        gridLayout_ray.addWidget(self.lineEdit_raySize,      1, 1, 1, 1)
        gridLayout_ray.addWidget(self.lab_SSD,               1, 2, 1, 1)
        gridLayout_ray.addWidget(self.lineEdit_SSD,          1, 3, 1, 1)
        #  gridLayout_ray.addWidget(self.lab_deepth,            2, 0, 1, 1)
        #  gridLayout_ray.addWidget(self.lineEdit_deepth,       2, 1, 1, 1)
        groupBox_ray = QGroupBox()
        groupBox_ray.setTitle('射线参数设置')
        groupBox_ray.setLayout(gridLayout_ray)
        groupBox_ray.setMaximumHeight(200)

        w = GetSystemMetrics (0)/1.2
        h = GetSystemMetrics (1)/1.2
        self.setFixedSize(w,h)

        self.drawWray = drawWidget()
        self.drawWray.pdd_oar_ray = 'ray'
        self.drawWray.setFixedSize(w*1/3, h*1.8/3)

        self.drawW = drawWidget()
        self.drawW.pdd_oar_ray = 'pdd'
        self.drawW.setFixedSize(w, h*1/3)
        self.drawW.updatePddRoad()

        layout_draw= QHBoxLayout()
        layout_draw.addWidget(self.drawW)
        layout_drawray= QHBoxLayout()
        layout_drawray.addWidget(self.drawWray)

        layout_left = QVBoxLayout()
        #  layout_left.addWidget(groupBox_PddOar)
        layout_left.addWidget(groupBox_ray)
        layout_left.addWidget(groupBox_road)

        self.grid = QGridLayout()
        #  self.grid.addWidget(groupBox_PddOar, 0,0,1,1)
        #  self.grid.addWidget(groupBox_ray,    1,0,1,1)
        #  self.grid.addWidget(groupBox_road,   2,0,1,1)
        layout_btn= QHBoxLayout()
        layout_btn.addWidget(self.btn_ok)
        layout_btn.addWidget(self.btn_cancel)
        self.grid.addLayout(layout_left,     0,0,1,1)
        self.grid.addLayout(layout_drawray,  0,1,1,1)
        self.grid.addLayout(layout_draw,     1,0,1,2)
        self.grid.addLayout(layout_btn,      2,1,1,1)
        self.setLayout(self.grid)

        self.default()
        self.select_row = 0

    def listViewClicked(self,index):
        self.select_row = index.row()
        t = self.listView.currentItem().text()
        target = t.split(' ')[3][:-2]
        step = t.split(' ')[4][5:]
        self.lineEdit_target.setText(target)
        self.lineEdit_step.setText(step)

    def PddOar(self):
        '''pdd、oar切换功能'''
        if self.radio_pdd.isChecked():
            self.drawW.pdd_oar_ray = 'pdd'
            self.step = [2,5,10]
            self.pos=[0,10,30,300]
        if self.radio_oar.isChecked():
            self.drawW.pdd_oar_ray = 'oar'
            self.raySizeChanged()
        self.reloadListView()

    def reloadListView(self):
        '''重置任务显示列表,并更新示意图显示'''
        self.listView.clear()
        for i in range(1,len(self.pos)):
            text = '%d mm -> %dmm step:%d'%(self.pos[i-1],self.pos[i],self.step[i-1])
            self.listView.addItem(text)
        if self.drawW.pdd_oar_ray == 'pdd':
            self.drawW.updatePddRoad(step=self.step,pos=self.pos)
        if self.drawW.pdd_oar_ray == 'oar':
            self.drawW.updateOarRoad(step=self.step,pos=self.pos)

    def default(self):
        self.step = [2,5,10]
        self.pos=[0,10,30,300]
        self.reloadListView()

    def add(self):
        '''添加任务'''
        target = self.lineEdit_target.text()
        if self.lineEdit_target.text() and self.lineEdit_step.text():
            target = int(self.lineEdit_target.text())
            s = int(self.lineEdit_step.text())
            if len(self.pos)<2:
                if not s<(target-self.pos[0]):
                    QMessageBox.information( self, '错误', '步距设置必须小于区间距离', QMessageBox.Yes)
                    return
                self.pos.insert(1,target)
                self.step.insert(0,s)
            else:
                for i in range(1,len(self.pos)):
                    if self.pos[i-1]<target<self.pos[i]:
                        if not s<(target-self.pos[i-1]):
                            QMessageBox.information( self, '错误', '步距设置必须小于区间距离', QMessageBox.Yes)
                            return
                        self.pos.insert(i,target)
                        self.step.insert(i-1,s)
                        break
                    elif self.pos[-1]<target:
                        if not s<(target-self.pos[-1]):
                            QMessageBox.information( self, '错误', '步距设置必须小于区间距离', QMessageBox.Yes)
                            return
                        self.pos.insert(len(self.pos),target)
                        self.step.insert(len(self.step),s)
                        break
                    elif target==self.pos[i]:
                        if not s<(target-self.pos[i-1]):
                            QMessageBox.information( self, '错误', '步距设置必须小于区间距离', QMessageBox.Yes)
                            return
                        if not QMessageBox.information( self, '警告', '路径重复,是否要修改步距?', QMessageBox.Yes|QMessageBox.No) == 65536:
                            self.step[i-1] = s
                        else:
                            return
            self.reloadListView()
        else:
            QMessageBox.information( self, '错误', '目标和步距设置错误', QMessageBox.Yes)
            return

    def dec(self):
        '''删除选中任务'''
        n = self.select_row
        # n = self.listView.currentRow()
        del self.pos[n+1]
        del self.step[n]
        self.reloadListView()

    def raySizeChanged(self):
        '''射野尺寸改变，变化oar路线示意图'''
        if self.lineEdit_raySize.text():
            s = int(self.lineEdit_raySize.text())
            if s < 2:
                s = 2
            elif s >60:
                s = 20
            self.drawWray.updateRaySize(s)

    def SSDChanged(self):
        if self.lineEdit_SSD.text():
            ssd = int(self.lineEdit_SSD.text())
            if ssd > 1:
                self.drawWray.updateSSD(ssd)

    def accept(self):
        Electrometer= self.comboBox_Electrometer.currentText()
        Power = self.comboBox_Power.currentText()
        SSD = self.lineEdit_SSD.text()
        RaySize = self.lineEdit_raySize.text()
        Type = 'pdd'
        Road = []
        Road.append([0,0,0,0,0,0])
        for i in range(1,len(self.pos)):
            Road.append([0,0,self.pos[i],0,0,self.step[i-1]])
        self.task = {
            'Electrometer':Electrometer,
            'Power': Power,
            'Type':  Type,
            'SSD':   SSD,
            'Type':  Type,
            'RaySize':RaySize,
            'Road':Road,
        }
        QDialog.accept(self)

    def reject(self):
        QDialog.reject(self)

class OarDialog(QDialog):
    def __init__(self,parent=None):
        super(QDialog, self).__init__(parent)
        #  import PyQt5_stylesheets
        #  self.setStyleSheet(PyQt5_stylesheets.load_stylesheet_pyqt5(style="style_blue"))
        self.resize(QSize(600,400))
        self.setWindowTitle('OAR任务设置')

        self.btn_ok = QPushButton('确认')
        self.btn_ok.clicked.connect(self.accept)
        self.btn_cancel = QPushButton('取消')
        self.btn_cancel.clicked.connect(self.reject)

        self.radio_pdd = QRadioButton('pdd')
        self.radio_pdd.setChecked(True)
        self.radio_pdd.toggled.connect(self.PddOar)
        self.radio_oar = QRadioButton('oar')
        self.radio_oar.toggled.connect(self.PddOar)

        Layout_PddOar = QHBoxLayout()
        Layout_PddOar.addWidget(self.radio_pdd)
        Layout_PddOar.addWidget(self.radio_oar)
        groupBox_PddOar = QGroupBox()
        groupBox_PddOar.setTitle('测量模式')
        groupBox_PddOar.setLayout(Layout_PddOar)
        groupBox_PddOar.setMaximumHeight(100)

        self.lab_target = QLabel('目标(mm):')
        self.lineEdit_target = QLineEdit()
        self.lab_step= QLabel('步距(mm):')
        self.lineEdit_step = QLineEdit()
        self.listView = QListWidget()
        self.listView.clicked.connect(self.listViewClicked)
        # 限制输入框只能输入数字
        self.lineEdit_step.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_step))
        self.lineEdit_target.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_target))

        self.btn_add = QPushButton('添加')
        self.btn_add.clicked.connect(self.add)
        self.btn_dec = QPushButton('移除')
        self.btn_dec.clicked.connect(self.dec)
        self.btn_default = QPushButton('恢复默认')
        self.btn_default.clicked.connect(self.default)

        gridLayout_road = QGridLayout()
        gridLayout_road.addWidget(self.lab_target,      1, 0, 1, 1)
        gridLayout_road.addWidget(self.lineEdit_target, 1, 1, 1, 1)
        gridLayout_road.addWidget(self.lab_step,        1, 2, 1, 1)
        gridLayout_road.addWidget(self.lineEdit_step,   1, 3, 1, 1)
        gridLayout_road.addWidget(self.btn_add,         1, 4, 1, 1)
        gridLayout_road.addWidget(self.btn_dec,         1, 5, 1, 1)
        gridLayout_road.addWidget(self.btn_default,     1, 6, 1, 1)
        gridLayout_road.addWidget(self.listView,        2, 0, 2, 7)
        groupBox_road = QGroupBox()
        groupBox_road.setTitle('走位路线设置')
        groupBox_road.setLayout(gridLayout_road)

        self.lab_Electrometer= QLabel('光机:')
        self.comboBox_Electrometer= QComboBox()
        self.comboBox_Electrometer.addItem('电子')
        self.comboBox_Electrometer.addItem('Co')
        self.comboBox_Electrometer.addItem('光机')
        self.lab_Power = QLabel('能量(MV/MeV):')
        self.comboBox_Power = QComboBox()
        self.comboBox_Power.addItem('1.0')
        self.comboBox_Power.addItem('1.2')
        self.comboBox_Power.addItem('2.0')

        self.lab_raySize = QLabel('射野(2-60cm):')
        self.lineEdit_raySize = QLineEdit('10')
        self.lineEdit_raySize.textChanged.connect(self.raySizeChanged)
        self.lineEdit_raySize.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_raySize))
        self.lab_SSD = QLabel('源波距(cm):')
        self.lineEdit_SSD = QLineEdit('100')
        self.lineEdit_SSD.textChanged.connect(self.SSDChanged)
        self.lineEdit_SSD.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_SSD))
        self.lab_deepth = QLabel('oar测量深度(mm):')
        self.lineEdit_deepth = QLineEdit('100')
        self.lineEdit_deepth.setValidator(QRegExpValidator(QRegExp("[0-9]+$"), self.lineEdit_deepth))
        self.lab_direction= QLabel('方向:')
        self.comboBox_direction= QComboBox()
        self.comboBox_direction.addItems(['东->西','西->东','北->南','南->北','东北->西南','西南->东北','西北->东南','东南->西北'])
        self.comboBox_direction.currentIndexChanged.connect(self.directionChanged)

        gridLayout_ray = QGridLayout()
        gridLayout_ray.addWidget(self.lab_Electrometer,      0, 0, 1, 1)
        gridLayout_ray.addWidget(self.comboBox_Electrometer, 0, 1, 1, 1)
        gridLayout_ray.addWidget(self.lab_Power,             0, 2, 1, 1)
        gridLayout_ray.addWidget(self.comboBox_Power,        0, 3, 1, 1)
        gridLayout_ray.addWidget(self.lab_raySize,           1, 0, 1, 1)
        gridLayout_ray.addWidget(self.lineEdit_raySize,      1, 1, 1, 1)
        gridLayout_ray.addWidget(self.lab_SSD,               1, 2, 1, 1)
        gridLayout_ray.addWidget(self.lineEdit_SSD,          1, 3, 1, 1)
        gridLayout_ray.addWidget(self.lab_deepth,            2, 0, 1, 1)
        gridLayout_ray.addWidget(self.lineEdit_deepth,       2, 1, 1, 1)
        gridLayout_ray.addWidget(self.lab_direction,         2, 2, 1, 1)
        gridLayout_ray.addWidget(self.comboBox_direction,    2, 3, 1, 1)
        groupBox_ray = QGroupBox()
        groupBox_ray.setTitle('射线参数设置')
        groupBox_ray.setLayout(gridLayout_ray)
        groupBox_ray.setMaximumHeight(200)

        w = GetSystemMetrics (0)/1.2
        h = GetSystemMetrics (1)/1.2

        self.drawWray = drawWidget()
        self.drawWray.pdd_oar_ray = 'ray'
        self.drawWray.setFixedSize(w*1/3, h*1.8/3)

        self.drawDirection = drawWidget()
        self.drawDirection.pdd_oar_ray = 'direction'
        self.drawDirection.setFixedSize(w*1/3, h*1/3)

        self.drawW = drawWidget()
        self.drawW.pdd_oar_ray = 'oar'
        self.drawW.setFixedSize(w*2/3, h*1/3)


        self.setFixedSize(w,h)
        #  self.drawW.updatePddRoad()

        layout_draw= QHBoxLayout()
        layout_draw.addWidget(self.drawW)
        layout_draw.addWidget(self.drawDirection)

        layout_drawray= QHBoxLayout()
        layout_drawray.addWidget(self.drawWray)

        layout_left = QVBoxLayout()
        #  layout_left.addWidget(groupBox_PddOar)
        layout_left.addWidget(groupBox_ray)
        layout_left.addWidget(groupBox_road)

        self.grid = QGridLayout()
        #  self.grid.addWidget(groupBox_PddOar, 0,0,1,1)
        #  self.grid.addWidget(groupBox_ray,    1,0,1,1)
        #  self.grid.addWidget(groupBox_road,   2,0,1,1)
        layout_btn= QHBoxLayout()
        layout_btn.addWidget(self.btn_ok)
        layout_btn.addWidget(self.btn_cancel)
        self.grid.addLayout(layout_left,     0,0,1,1)
        self.grid.addLayout(layout_drawray,  0,1,1,1)
        self.grid.addLayout(layout_draw,     1,0,1,2)
        self.grid.addLayout(layout_btn,      2,1,1,1)
        self.setLayout(self.grid)
        self.default()
        self.select_row = 0

    def listViewClicked(self,index):
        self.select_row = index.row()
        t = self.listView.currentItem().text()
        target = t.split(' ')[3][:-2]
        step = t.split(' ')[4][5:]
        self.lineEdit_target.setText(target)
        self.lineEdit_step.setText(step)

    def PddOar(self):
        '''pdd、oar切换功能'''
        if self.radio_pdd.isChecked():
            self.drawW.pdd_oar_ray = 'pdd'
            self.step = [2,5,10]
            self.pos=[0,10,30,300]
        if self.radio_oar.isChecked():
            self.drawW.pdd_oar_ray = 'oar'
            self.raySizeChanged()
        self.reloadListView()

    def reloadListView(self):
        '''重置任务显示列表,并更新示意图显示'''
        self.listView.clear()
        for i in range(1,len(self.pos)):
            text = '%d mm -> %dmm step:%d'%(self.pos[i-1],self.pos[i],self.step[i-1])
            self.listView.addItem(text)
        if self.drawW.pdd_oar_ray == 'pdd':
            self.drawW.updatePddRoad(step=self.step,pos=self.pos)
        if self.drawW.pdd_oar_ray == 'oar':
            self.drawW.updateOarRoad(step=self.step,pos=self.pos)

    def default(self):
        '''恢复默认'''
        self.lineEdit_raySize.setText('10')
        s = int(self.lineEdit_raySize.text())
        self.step = [10,3,5]
        self.pos=[0,int(s*10/2-10),int(s*10/2+10),int(s*10/2+30)]
        self.reloadListView()
        self.directionChanged()

    def add(self):
        '''添加任务'''
        target = self.lineEdit_target.text()
        if self.lineEdit_target.text() and self.lineEdit_step.text():
            target = int(self.lineEdit_target.text())
            s = int(self.lineEdit_step.text())
            if len(self.pos)<2:
                if not s<(target-self.pos[0]):
                    QMessageBox.information( self, '错误', '步距设置必须小于区间距离', QMessageBox.Yes)
                    return
                self.pos.insert(1,target)
                self.step.insert(0,s)
            else:
                for i in range(1,len(self.pos)):
                    if self.pos[i-1]<target<self.pos[i]:
                        if not s<(target-self.pos[i-1]):
                            QMessageBox.information( self, '错误', '步距设置必须小于区间距离', QMessageBox.Yes)
                            return
                        self.pos.insert(i,target)
                        self.step.insert(i-1,s)
                        break
                    elif self.pos[-1]<target:
                        if not s<(target-self.pos[-1]):
                            QMessageBox.information( self, '错误', '步距设置必须小于区间距离', QMessageBox.Yes)
                            return
                        self.pos.insert(len(self.pos),target)
                        self.step.insert(len(self.step),s)
                        break
                    elif target==self.pos[i]:
                        if not s<(target-self.pos[i-1]):
                            QMessageBox.information( self, '错误', '步距设置必须小于区间距离', QMessageBox.Yes)
                            return
                        if not QMessageBox.information( self, '警告', '路径重复,是否要修改步距?', QMessageBox.Yes|QMessageBox.No) == 65536:
                            self.step[i-1] = s
                        else:
                            return
            self.reloadListView()
        else:
            QMessageBox.information( self, '错误', '目标和步距设置错误', QMessageBox.Yes)
            return

    def dec(self):
        '''删除选中任务'''
        n = self.select_row
        del self.pos[n+1]
        del self.step[n]
        self.reloadListView()

    def raySizeChanged(self):
        '''射野尺寸改变，变化oar路线示意图'''
        if self.lineEdit_raySize.text():
            s = int(self.lineEdit_raySize.text())
            self.pos=[0,int(s*10/2-10),int(s*10/2+10),int(s*10/2+30)]
            if s <= 2:
                s = 2
                self.step=[1,1,5]
                self.pos=[0,int(s*10/2-5),int(s*10/2+5),int(s*10/2+30)]
            elif s >60:
                s = 60
                self.step=[10,3,5]
                self.pos=[0,int(s*10/2-10),int(s*10/2+10),int(s*10/2+30)]
            else:
                self.step=[10,3,5]
                self.pos=[0,int(s*10/2-10),int(s*10/2+10),int(s*10/2+30)]
            self.reloadListView()
            self.drawWray.updateRaySize(s)
            self.drawW.raySize=s

    def SSDChanged(self):
        if self.lineEdit_SSD.text():
            ssd = int(self.lineEdit_SSD.text())
            if ssd > 1:
                self.drawWray.updateSSD(ssd)

    def directionChanged(self):
        '''方向变化'''
        self.drawDirection.direction = self.comboBox_direction.currentText()
        self.drawDirection.update()

    def accept(self):
        Electrometer= self.comboBox_Electrometer.currentText()
        Power = self.comboBox_Power.currentText()
        SSD = self.lineEdit_SSD.text()
        RaySize = self.lineEdit_raySize.text()
        Type = 'oar'
        direction = self.comboBox_direction.currentText()
        deepth= self.lineEdit_deepth.text()
        Road = []
        end = len(self.pos)
        for i in range(1,end):
            if direction=='东->西':
                if i==1:
                    Road.append([self.pos[end-i],0,int(deepth),0,0,0])
                    Road.insert(i,[-self.pos[end-i],0,int(deepth),self.step[end-i-1],0,0])
                else:
                    Road.insert(i-1,[self.pos[end-i],0,int(deepth),self.step[end-i],0,0])
                    Road.insert(i,[-self.pos[end-i],0,int(deepth),self.step[end-i-1],0,0])
            if direction=='西->东':
                if i==1:
                    Road.append([-self.pos[end-i],0,int(deepth),0,0,0])
                    Road.insert(i,[self.pos[end-i],0,int(deepth),self.step[end-i-1],0,0])
                else:
                    Road.insert(i-1,[-self.pos[end-i],0,int(deepth),self.step[end-i],0,0])
                    Road.insert(i,[self.pos[end-i],0,int(deepth),self.step[end-i-1],0,0])
            if direction=='南->北':
                if i==1:
                    Road.append([0,-self.pos[end-i],int(deepth),0,0,0])
                    Road.insert(i,[0,self.pos[end-i],int(deepth),0,self.step[end-i-1],0])
                else:
                    Road.insert(i-1,[0,-self.pos[end-i],int(deepth),0,self.step[end-i],0])
                    Road.insert(i,[0,self.pos[end-i],int(deepth),0,self.step[end-i-1],0])
            if direction=='北->南':
                if i==1:
                    Road.append([0,self.pos[end-i],int(deepth),0,0,0])
                    Road.insert(i,[0,-self.pos[end-i],int(deepth),0,self.step[end-i-1],0])
                else:
                    Road.insert(i-1,[0,self.pos[end-i],int(deepth),0,self.step[end-i],0])
                    Road.insert(i,[0,-self.pos[end-i],int(deepth),0,self.step[end-i-1],0])
            if direction=='东北->西南':
                if i==1:
                    Road.append([self.pos[end-i],self.pos[end-i],int(deepth),0,0,0])
                    Road.insert(i,[-self.pos[end-i],-self.pos[end-i],int(deepth),self.step[end-i-1],self.step[end-i-1],0])
                else:
                    Road.insert(i-1,[self.pos[end-i],self.pos[end-i],int(deepth),self.step[end-i],self.step[end-i],0])
                    Road.insert(i,[-self.pos[end-i],-self.pos[end-i],int(deepth),self.step[end-i-1],self.step[end-i-1],0])
            if direction=='西南->东北':
                if i==1:
                    Road.append([-self.pos[end-i],-self.pos[end-i],int(deepth),0,0,0])
                    Road.insert(i,[self.pos[end-i],self.pos[end-i],int(deepth),self.step[end-i-1],self.step[end-i-1],0])
                else:
                    Road.insert(i-1,[-self.pos[end-i],-self.pos[end-i],int(deepth),self.step[end-i],self.step[end-i],0])
                    Road.insert(i,[self.pos[end-i],self.pos[end-i],int(deepth),self.step[end-i-1],self.step[end-i-1],0])
            if direction=='东南->西北':
                if i==1:
                    Road.append([self.pos[end-i],-self.pos[end-i],int(deepth),0,0,0])
                    Road.insert(i,[-self.pos[end-i],self.pos[end-i],int(deepth),self.step[end-i-1],self.step[end-i-1],0])
                else:
                    Road.insert(i-1,[self.pos[end-i],-self.pos[end-i],int(deepth),self.step[end-i],self.step[end-i],0])
                    Road.insert(i,[-self.pos[end-i],self.pos[end-i],int(deepth),self.step[end-i-1],self.step[end-i-1],0])
            if direction=='西北->东南':
                if i==1:
                    Road.append([-self.pos[end-i],self.pos[end-i],int(deepth),0,0,0])
                    Road.insert(i,[self.pos[end-i],-self.pos[end-i],int(deepth),self.step[end-i-1],self.step[end-i-1],0])
                else:
                    Road.insert(i-1,[-self.pos[end-i],self.pos[end-i],int(deepth),self.step[end-i],self.step[end-i],0])
                    Road.insert(i,[self.pos[end-i],-self.pos[end-i],int(deepth),self.step[end-i-1],self.step[end-i-1],0])
        self.task = {
            'Electrometer': Electrometer,
            'Power':        Power,
            'Type':         Type,
            'SSD':          SSD,
            'Type':         Type,
            'RaySize':      RaySize,
            'Road':         Road,
            'Deepth':       deepth,
            'Direction':    direction,
        }
        print(self.task)
        QDialog.accept(self)

    def reject(self):
        QDialog.reject(self)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = OarDialog()
    window.show()
    window.resize(100, 100)
    app.exec_()
    # sys.exit(app.exec_())
